package com.wffz.controller;

import com.alibaba.fastjson.JSONObject;
import com.wffz.comm.api.ResponseResult;
import com.wffz.config.redis.RedisDelayQueueEnum;
import com.wffz.config.redis.RedisDelayQueueUtil;
import com.wffz.dto.BetDTO;
import com.wffz.po.Bet;
import com.wffz.po.ClientUser;
import com.wffz.po.MoneyLog;
import com.wffz.service.BetService;
import com.wffz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author sunlei
 * @className BetController
 * @Description
 * @date 2023-02-18 16:30
 **/
@Slf4j
@RestController
@RequestMapping("/bet")
public class BetController {


    @Resource
    private UserService userService;


    @Resource
    private BetService betService;


    @Autowired
    private RedisDelayQueueUtil redisDelayQueueUtil;

    /**
     * @Description: [用户下注]
     * @Title: userBet
     * @Author: SUNLEI
     * @Date: 2023-02-18
     * @Param:
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @PostMapping("/userBet")
    public ResponseResult userBet(@RequestBody BetDTO dto, HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        Bet bet = betService.saveBetLJ(dto, clientUser);
        redisDelayQueueUtil.addDelayQueue(bet, dto.getCountDown(), TimeUnit.SECONDS, RedisDelayQueueEnum.TWO_SECOND_CONSUME_QUEUE.getQueueName());
        Map m = new HashMap();
        m.put("bet",bet);
        m.put("clientUser",clientUser);
        return ResponseResult.success(m);
    }

    @GetMapping("/queryOrderList")
    public ResponseResult queryOrderList(@RequestParam(value = "showType" , required = true, defaultValue = "1") Integer showType, HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        List<Bet> list = betService.queryOrderList(showType, clientUser);
        Map m = new HashMap();
        m.put("betList" , list);
        m.put("clientUser" , clientUser);
        return ResponseResult.success(m);
    }
}
